﻿using Kairos.Models;
using Kairos.Properties;
using PagedList;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace Kairos.Controllers
{
    public class ImageController : Controller
    {
        private myContext db = new myContext();

        //
        // GET: /Image/

        [AccessControl(Roles.Administrator)]
        public ActionResult Index(int? page)
        {
            int pageNumber = (page ?? 1);

            return View(db.Images.OrderBy(image => image.FileName).ToPagedList(pageNumber, Settings.Default.PageSize));
        }

        //
        // GET: /Image/Details/5

        [AccessControl(Roles.Administrator)]
        public ActionResult Details(int id = 0)
        {
            Image image = db.Images.Find(id);
            if (image == null)
            {
                return HttpNotFound();
            }
            return View(image);
        }

        //
        // GET: /Image/Data/5

        [AccessControl(Roles.All)]
        public ActionResult Data(int id = 0)
        {
            Image image = db.Images.Find(id);
            if (image == null)
            {
                return HttpNotFound();
            }
            return View(image);
        }

        //
        // GET: /Image/Create

        [AccessControl(Roles.Administrator)]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Image/Create

        [HttpPost]
        // TODO: Trouver un moyen de réactiver le tag ValidateAntiForgeryToken
        //[ValidateAntiForgeryToken] // CkEditor
        [AccessControl(Roles.Administrator)]
        public ActionResult Create(Image image, int? CKEditorFuncNum)
        {
            if (image == null && CKEditorFuncNum == null)
                return RedirectToAction("Create");

            if (ModelState.IsValid)
            {
                db.Images.Add(image);
                db.SaveChanges();

                if (CKEditorFuncNum == null)
                    return RedirectToAction("Details", new { id = image.ID });

                return Content(string.Format(
                    "<script>window.parent.CKEDITOR.tools.callFunction({0}, \"{1}\");</script>",
                    CKEditorFuncNum,
                    Url.Action("Data", "Image", new { id = image.ID })));
            }

            return View(image);
        }

        //
        // GET: /Image/Edit/5

        [AccessControl(Roles.Administrator)]
        public ActionResult Edit(int id = 0)
        {
            Image image = db.Images.Find(id);
            if (image == null)
            {
                return HttpNotFound();
            }
            return View(image);
        }

        //
        // POST: /Image/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator)]
        public ActionResult Edit(Image image, int id)
        {
            if (image == null)
                return RedirectToAction("Edit", new { id });

            image.ID = id;

            if (ModelState.IsValid)
            {
                db.Entry(image).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", new { id = image.ID });
            }
            return View(image);
        }

        //
        // GET: /Image/Delete/5

        [AccessControl(Roles.Administrator)]
        public ActionResult Delete(int id = 0)
        {
            Image image = db.Images.Find(id);
            if (image == null)
            {
                return HttpNotFound();
            }
            return View(image);
        }

        //
        // POST: /Image/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator)]
        public ActionResult DeleteConfirmed(int id)
        {
            Image image = db.Images.Find(id);
            db.Images.Remove(image);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}